package org.csu.secondhand.api.controller.fronted;

import org.csu.secondhand.api.entity.Cart;
import org.csu.secondhand.api.service.CartService;
import org.csu.secondhand.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("getCartByUser")
    @ResponseBody
    public Map<Object, Object> getCartByUser(String user) {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", cartService.getCartByUser(user));
        return map;
    }

    @PostMapping("insertCart")
    @ResponseBody
    public Map<Object, Object> insertCart(String user, long dealing) {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", cartService.insertCart(new Cart(0, TimeUtil.getTime(), user, dealing)));
        return map;
    }

    @PostMapping("deleteCart")
    @ResponseBody
    public Map<Object, Object> deleteCart(String user, long dealing) {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", cartService.removeCart(user, dealing));
        return map;
    }

    @GetMapping("isInCart")
    @ResponseBody
    public Map<Object, Object> isInCart(String user, long dealing) {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", cartService.isInCart(user, dealing));
        return map;
    }

}
